/*
 * Copyright (c) 2010, okazoh_tk. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 *  - Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 *  - Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 *  - Neither the name of the copyright holder nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#include "alkes/testdriver/TestCase.h"
#include "alkes/testdriver/Graphics3DTestDriver.h"

namespace alkes {

Graphics3DTestDriver::Graphics3DTestDriver(IGraphics3D* graphics)
: Graphics2DTestDriver(graphics)
, graphics3d_(graphics)
{
}

Graphics3DTestDriver::~Graphics3DTestDriver()
{
}

void Graphics3DTestDriver::testDefaultRenderTarget3D()
{
    IRenderTarget3D* p1 = graphics3d_->createDefaultRenderTarget3D();
    IRenderTarget3D* p2 = graphics3d_->createDefaultRenderTarget3D();

    AL_TC_ASSERT(p1);
    AL_TC_ASSERT(p2);

    if (p1) p1->release();
    if (p2) p2->release();
}

void Graphics3DTestDriver::testVertexBuffer()
{
    IVertexBuffer* p = graphics3d_->createVertexBuffer(0);

#define CLEANUP()   if (p) p->release()

    // invalid type
    AL_TC_EQUALS(p, 0);
    CLEANUP();

    Vector2D vertex[] = {
        {0.1f, 0.1f},
        {0.1f, 0.2f},
        {0.1f, 0.3f},
        {0.1f, 0.4f},
        {999.0f, 1000.f},
    };
    Color colors[] = {
        {0xff, 0x00, 0x00, 0xff},
        {0x00, 0xff, 0x00, 0xff},
        {0x00, 0x00, 0xff, 0xff},
        {0xff, 0xff, 0xff, 0xff},
        {0x00, 0x00, 0x00, 0x00},
    };
    Vector2D coords[] = {
        {0.0f, 0.0f},
        {1.0f, 0.0f},
        {0.0f, 1.0f},
        {1.0f, 1.0f},
        {0.5f, 0.1f},
    };

    // xy only
    p = graphics3d_->createVertexBuffer(AL_VERTEX_XY);
    AL_TC_DIFFERS(p, 0);
    if (p)
    {
        int32_t len = sizeof(vertex)/sizeof(vertex);
        p->setVertex2D(0, 1, vertex);
        p->setVertex2D(1, len-1, vertex+1);
        p->setVertex2D(len - 1, len, vertex); // overrun check
    }
    CLEANUP();

    // color only
    p = graphics3d_->createVertexBuffer(AL_VERTEX_COLOR);
    AL_TC_DIFFERS(p, 0);
    if (p)
    {
        int32_t len = sizeof(colors)/sizeof(colors);
        p->setDiffuse(0, 1, colors);
        p->setDiffuse(1, len-1, colors+1);
        p->setDiffuse(len - 1, len, colors); // overrun check
    }
    CLEANUP();

    // xy and color
    p = graphics3d_->createVertexBuffer(AL_VERTEX_XY|AL_VERTEX_COLOR);
    AL_TC_DIFFERS(p, 0);
    if (p)
    {
        int32_t len = sizeof(vertex)/sizeof(vertex);
        p->setVertex2DDiffuse(0, 1, vertex, colors);
        p->setVertex2DDiffuse(1, len-1, vertex+1, colors+1);
        p->setVertex2DDiffuse(len - 1, len, vertex, colors); // overrun check
    }
    CLEANUP();

    // xy and uv1
    p = graphics3d_->createVertexBuffer(AL_VERTEX_XY|AL_VERTEX_UV1);
    AL_TC_DIFFERS(p, 0);
    if (p)
    {
        int32_t len = sizeof(vertex)/sizeof(vertex);
        p->setVertex2DTexCoord(0, 1, vertex, coords);
        p->setVertex2DTexCoord(1, len-1, vertex+1, coords+1);
        p->setVertex2DTexCoord(len - 1, len, vertex, coords); // overrun check
    }
    CLEANUP();
}

}
